<template>
	<div>
		<el-card shadow="hover">
			<div class=" mb15">
				<el-form :model="tableData.param" ref="queryRef" :inline="true" label-width="100px">
					<el-row>
						<el-col :span="8" class="colBlock">
							<el-form-item label="登录时间">
								<el-date-picker v-model="tableData.dateRange" type="datetimerange" size="default"
									style="width: 300px" range-separator="-" start-placeholder="开始日期"
									end-placeholder="结束日期"></el-date-picker>
							</el-form-item>
						</el-col>
						<el-col :span="8" class="colBlock">
							<el-form-item label="登录人员" prop="userName">
								<el-input v-model="tableData.param.userName" placeholder="请输入登录人员" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="登录状态" prop="result">
								<el-select v-model="tableData.param.result" placeholder="请选择登录状态" clearable>
									<el-option label="成功" :value="1" />
									<el-option label="失败" :value="0" />
								</el-select>
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item label="登录IP" prop="loginIp">
								<el-input v-model="tableData.param.loginIp" placeholder="请输入登录IP" clearable
									@keyup.enter.native="getList" />
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="!showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" link @click="toggleSearch">
									{{ word }}
									<el-icon v-show="showAll">
										<ArrowUp />
									</el-icon>
									<el-icon v-show="!showAll">
										<ArrowDown />
									</el-icon>
								</el-button>
							</el-form-item>
						</el-col>
						<el-col :span="8" :class="showAll ? 'colBlock' : 'colNone'">
							<el-form-item>
								<el-button type="primary" @click="getList"><el-icon>
										<Search />
									</el-icon>搜索</el-button>
								<el-button @click="resetQuery(queryRef)"><el-icon>
										<Refresh />
									</el-icon>重置</el-button>
								<el-button type="primary" link @click="toggleSearch">
									{{ word }}
									<el-icon v-show="showAll"><ArrowUp /></el-icon>
									<el-icon v-show="!showAll"><ArrowDown /></el-icon>
								</el-button>
							</el-form-item>
						</el-col>
					</el-row>
				</el-form>
				<el-row :gutter="10" class="mb8" v-if="permissionStore.isPermissionsBtn('001007001')">
					<el-col :span="1.5">
						<el-button type="primary" @click="exportFile"><el-icon><Download /></el-icon> 导出</el-button>
					</el-col>
				</el-row>
			</div>
			<el-table v-loading="loading" :data="tableData.data">
				<el-table-column label="日志编号" align="center" prop="id" min-width="100px" />
				<el-table-column label="登录人员" align="center" prop="userName" min-width="100px" />
				<el-table-column label="登录IP" align="center" prop="loginIp" min-width="100px" />
				<el-table-column label="登录地点" align="center" prop="loginAddress" min-width="100px" />
				<el-table-column label="登录状态" align="center" min-width="100px">
					<template #default="scope">
						<el-tag type="success" v-if="scope.row.result === 1">成功</el-tag>
						<el-tag type="danger" v-else>失败</el-tag>
					</template>
				</el-table-column>
				<el-table-column label="登录时间" align="center" prop="loginTimeStr" min-width="100px" />
			</el-table>
			<pagination v-show="tableData.total > 0" :total="tableData.total" v-model:page="tableData.param.pageNumber"
				v-model:limit="tableData.param.pageLength" @pagination="getList" />
		</el-card>
	</div>
</template>
<script lang="ts">
import { loginLogListApi } from '@/api/auth/loginLog';
import { toRefs, reactive, onMounted, ref, defineComponent, computed, getCurrentInstance, toRaw } from 'vue';
import { FormInstance } from 'element-plus';
import { usePermissionStore } from "@/store/modules/permission"
import {getFile} from '@/utils/excel/excelUtil'
import {  updateParamsWithDateRange } from '@/utils/index'
export default defineComponent({
	name: 'loginLogList',
	setup() {
		const { proxy } = <any>getCurrentInstance();
		const loading = ref(false);
		const queryRef = ref();
		const addRef = ref();
		const editRef = ref();
		const detailRef = ref();
		const permissionStore = usePermissionStore()
		// 是否显示所有搜索选项
		const showAll = ref(false);

		const word = computed(() => {
			if (showAll.value === false) {
				//对文字进行处理
				return '展开搜索';
			} else {
				return '收起搜索';
			}
		});
		const state = reactive({
			ids: [],
			tableData: {
				data: [],
				total: 0,
				loading: false,
				dateRange: [],
				param: {
					pageNumber: 1,
					pageLength: 10,
					beginTime: null,
					endTime: null,
					userName: null,
					result: null,
					loginIp: null
				},
			},
		});
		// 页面加载时
		onMounted(() => {
			initTableData();
		});
		// 初始化表格数据
		const initTableData = () => {
			getList();
		};
		/** 重置按钮登录 */
		const resetQuery = (formEl: FormInstance | undefined) => {
			if (!formEl) return;
			formEl.resetFields();
			state.tableData.dateRange = []
			getList();
		};
		// 获取列表数据
		const getList = () => {
			loading.value = true;
			const obj = updateParamsWithDateRange(state.tableData.dateRange)
			Object.assign(state.tableData.param, obj);
			loginLogListApi(state.tableData.param).then((res: any) => {
				let list = res.data.list ?? [];
				state.tableData.data = list
				console.log(state.tableData.data);
				state.tableData.total = res.data.total;
				loading.value = false;
			});
		};
		const toggleSearch = () => {
			showAll.value = !showAll.value;
		};
		// 导出文件
		const exportFile = async () => {
			getFile('/system/login/log/export','登录日志')
		}
		return {
			proxy,
			addRef,
			editRef,
			detailRef,
			showAll,
			loading,
			word,
			queryRef,
			permissionStore,
			resetQuery,
			getList,
			toggleSearch,
			exportFile,
			...toRefs(state),
		};
	},
});
</script>
<style lang="scss" scoped>
.colBlock {
	display: block;
}

.colNone {
	display: none;
}
</style>
